<template>
    <el-aside width="180px">
        <el-menu background-color="black" text-color="#fff">
            <h3>好友列表</h3>
            <el-menu-item v-for="item in userStore.state.friendList" :index="item.updated" :key="item.id" @click="friendUpdateHeaderStore(item)">
                <component class="icons" is="house"></component>
                <span>{{ item.name }}</span>
            </el-menu-item>
            <el-menu-item v-for="item in userStore.state.groupList" :index="item.updated" :key="item.id" @click="groupUpdaetHeaderStore(item)">
                <component class="icons" is="house"></component>
                <span>{{ item.name }}</span>
            </el-menu-item>
        </el-menu>
    </el-aside>
</template>

<script setup>
import { ref, computed, onMounted } from "vue"
import { useHeaderStore } from "../stores/header";
import { useUserStore } from "../stores/user";
import chatApi from "../api/chat.js";
import { useWebSocketStore } from "../stores/websocket.js";

const ws = useWebSocketStore();
// const groupList = ref([])
// const friendList = ref([])
const headerStore = useHeaderStore();
const userStore = useUserStore();

const handleOpen = (key, keyPath) => {
    console.log(key, keyPath)
}
const handleClose = (key, keyPath) => {
    console.log(key, keyPath)
}
// 点击好友触发
const friendUpdateHeaderStore = async (item) => {
    headerStore.state.type = "friend";
    headerStore.state.name = item.name;
    // headerStore.state.id = item.id;
    headerStore.state.id = item.id; // 好友id
    headerStore.state.friendship_id = item.id;
    headerStore.state.friend_id = item.friend_id;

    // 获取聊天记录
    ws.send({
        method: "get_friend",
        id: item.id
    })

}

// 点击群组触发
const groupUpdaetHeaderStore = (item) => {
    headerStore.state.type = "group";
    headerStore.state.name = item.name;
    headerStore.state.id = item.id; // 群组id
    ws.send({
        method: "get_group",
        id: item.id
    })
}

</script>

<style lang="less" scoped>
.icons {
    width: 18px;
    height: 18px;
    margin-right: 5px;
}

.el-menu {
    border-right:none;
    h3{
        line-height: 48px;
        color: #fff;
        text-align: center;
        margin: 0;
    }
}

.el-aside {
    height: 100%;
    background-color: black;
}
</style>